// http://blog.csdn.net/Solstice/archive/2011/02/12/6181488.aspx
// https://github.com/chenshuo/recipes/blob/master/thread/test/ExitDeadLock.cc

#include <stdlib.h>
#include <pthread.h>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

void someFunCallExit()
{
	exit(1);
	// _exit( 0 );
}

class GlobalObject
{
public:
	~GlobalObject()
	{
		std::cout << "GlobalObject::~GlobalObject" << std::endl;
		{
			pthread_mutex_lock( &m_mutex );
			boost::shared_ptr< void > lock_guard( (void *)NULL, boost::bind( pthread_mutex_unlock, &m_mutex ) );
			// do something clean up
		}
		std::cout << "GlobalObject::~GlobalObject done" << std::endl;
	}
	void DoIt()
	{
		pthread_mutex_lock( &m_mutex );
		boost::shared_ptr< void > lock_guard( (void *)NULL, boost::bind( pthread_mutex_unlock, &m_mutex ) );
		someFunCallExit();
	}

private:
	pthread_mutex_t m_mutex;
};

GlobalObject g_obj;

int main( int argc, char *argv[] )
{
	g_obj.DoIt();
	return 0;
}
